package com.goodjob.musicplayer.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


import com.goodjob.musicplayer.R;
import com.goodjob.musicplayer.entity.LoginBean;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText name;
    private EditText password;
    private EditText passwordagin;
    private Button btn_zhuce;
    List<LoginBean> loginBeanList=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
    }

    private void initView() {
        LitePal.getDatabase();
        name = (EditText) findViewById(R.id.name);
        password = (EditText) findViewById(R.id.password);
        passwordagin = (EditText) findViewById(R.id.passwordagin);
        btn_zhuce = (Button) findViewById(R.id.btn_zhuce);

        btn_zhuce.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_zhuce:
                submit();
                break;
        }
    }

    private void submit() {
        // validate
        String nameString = name.getText().toString().trim();
        if (TextUtils.isEmpty(nameString)) {
            Toast.makeText(this, "姓名不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String passwordString = password.getText().toString().trim();
        if (TextUtils.isEmpty(passwordString)) {
            Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        String passwordaginString = passwordagin.getText().toString().trim();
        if (TextUtils.isEmpty(passwordaginString)) {
            Toast.makeText(this, "再次输入密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        if (!passwordaginString.equals(passwordString)){
            Toast.makeText(this, "两次输入密码不一致", Toast.LENGTH_SHORT).show();
            return;
        }
        loginBeanList=LitePal.findAll(LoginBean.class);
        for (int i = 0; i < loginBeanList.size(); i++) {
            if (loginBeanList.get(i).getName().equals(nameString)){
                Toast.makeText(this, "用户名已存在，请直接登录", Toast.LENGTH_SHORT).show();
                return;
            }
        }
        LoginBean loginBean=new LoginBean();
        loginBean.setName(nameString);
        loginBean.setPassword(passwordString);
        loginBean.save();
        Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
        // TODO validate success, do something
        startActivity(new Intent(RegisterActivity.this,LoginActivity.class));
        finish();

    }
}
